<?php

namespace App\Services\common;

use App\Models\common\File;
use App\Services\BaseService;

/**
 * 附件 service
 *
 * @Description:
 *
 * @Author: zhx
 *
 * @Date: 2023-09-05
 *
 * @LastEditTime: 2023-09-05
 */
class FileService extends BaseService
{
    /**
     * 数据查询
     *
     * @return array
     */
    public static function getPageData($requestData)
    {
        [$perPage, $page, $orderBy, $orderDir] = [
            isset($requestData['perPage']) ? $requestData['perPage'] : 20,
            isset($requestData['page']) ? $requestData['page'] : 1,
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'desc',
        ];

        $models = File::query();
        if (isset($requestData['keywords'])) {
            $models->where('name', 'like', '%'.$requestData['keywords'].'%');
        }
        if (isset($requestData['uid'])) {
            $models->where('uid', $requestData['uid']);
        }
        if (isset($requestData['platform_id'])) {
            $models->where('platform_id', $requestData['platform_id']);
        }
        if (isset($requestData['type'])) {
            $models->where('type', $requestData['type']);
        }
        $list = $models->orderBy($orderBy, $orderDir)->paginate($perPage);

        return $list;
    }

    /**
     * 数据查询
     *
     * @return array
     */
    public static function getData($requestData)
    {
        [$orderBy, $orderDir] = [
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'desc',
        ];

        $models = File::query();
        if (isset($requestData['keywords'])) {
            $models->where('name', 'like', '%'.$requestData['keywords'].'%');
        }
        if (isset($requestData['uid'])) {
            $models->where('uid', $requestData['uid']);
        }
        if (isset($requestData['platform_id'])) {
            $models->where('platform_id', $requestData['platform_id']);
        }
        if (isset($requestData['type'])) {
            $models->where('type', $requestData['type']);
        }
        $list = $models->orderBy($orderBy, $orderDir)->get();

        return $list;
    }

    /**
     * 编辑
     *
     * @return \Illuminate\Http\JsonResponse
     * User: zhx
     * Date: 2023/6/30 14:28
     */
    public static function edit($id, $requestData)
    {
        $models = \App\Models\common\File::query();
        $models->where('id', $id);
        $result = $models->update($requestData);

        return $result;
    }

    /**
     * 添加
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface create
     */
    public static function create($requestData)
    {
        $result = File::create($requestData);

        return $result;
    }

    /**
     * 删除
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function destroy($id)
    {
        $models = File::query();
        $reult = $models->whereIn('id', $id)->delete();

        return $reult;
    }

    /**
     * 详细
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function info($id)
    {
        $models = File::query();

        return $models->where('id', $id)->first();
    }
}
